/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CylinderCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Sphere;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
 *
 * @author Ben
 */
public class SpawnController 
{
    LundWidget lund = new LundWidget();
    public Geometry spawnBox(SimpleApplication app, Vector3f loc)
    {
        Box temp = new Box(Vector3f.ZERO,Main.player.choiceSize,Main.player.choiceSize,Main.player.choiceSize);
        Geometry gTemp = new Geometry("Box",temp);
        RigidBodyControl gTempControl = new RigidBodyControl(new BoxCollisionShape(new Vector3f(Main.player.choiceSize,
                Main.player.choiceSize,Main.player.choiceSize)));
        Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        mat.setBoolean("UseMaterialColors", true);
        mat.setColor("Diffuse",ColorRGBA.Magenta);
        mat.setColor("Specular", ColorRGBA.White);
        mat.setFloat("Shininess", 12);
        mat.setBoolean("VertexLighting", false);
        mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Back);
        gTempControl.setKinematic(true);
        gTemp.setMaterial(mat);
        gTemp.addControl(gTempControl);
        gTemp.setShadowMode(ShadowMode.CastAndReceive);
        gTemp.setLocalTranslation(loc);
        PhysicsController.addChild(gTemp);
        gTemp.setName("Box");
        return gTemp;
    }
    
    public Geometry spawnSphere(SimpleApplication app, Vector3f loc)
    {
        Sphere temp2 = new Sphere(16, 16, Main.player.choiceSize);
        Geometry gTemp = new Geometry("Box",temp2);
        RigidBodyControl gTempControl = new RigidBodyControl(new SphereCollisionShape(Main.player.choiceSize), 2);
        Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        mat.setBoolean("UseMaterialColors", true);
        mat.setColor("Diffuse",ColorRGBA.Magenta);
        mat.setColor("Specular", ColorRGBA.White);
        mat.setFloat("Shininess", 12);
        mat.setBoolean("VertexLighting", false);
        mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Back);
        gTemp.setMaterial(mat);
        gTemp.addControl(gTempControl);
        gTempControl.setKinematic(true);
        gTemp.setShadowMode(ShadowMode.CastAndReceive);
        gTemp.setLocalTranslation(loc);
        PhysicsController.addChild(gTemp);
        gTemp.setName("Sphere");
        return gTemp;
    }
    
    public Geometry spawnCylinder(SimpleApplication app, Vector3f loc)
    {
        Cylinder temp3 = new Cylinder(6, 18, Main.player.choiceSize, Main.player.choiceSize, true);
        Geometry gTemp = new Geometry("Box",temp3);
        Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        RigidBodyControl gTempControl = new RigidBodyControl(new CylinderCollisionShape(new Vector3f(6, 18, 
                Main.player.choiceSize)));
        mat.setBoolean("UseMaterialColors", true);
        mat.setColor("Diffuse",ColorRGBA.Magenta);
        mat.setColor("Specular", ColorRGBA.White);
        mat.setFloat("Shininess", 12);
        mat.setBoolean("VertexLighting", false);
        mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Back);
        gTemp.addControl(gTempControl);
        gTempControl.setKinematic(true);
        gTemp.setMaterial(mat);
        gTemp.setShadowMode(ShadowMode.CastAndReceive);
        gTemp.setLocalTranslation(loc);
        PhysicsController.addChild(gTemp);
        gTemp.setName("Cylinder");
        return gTemp;
    }
    
    public Spatial createModel(SimpleApplication app, Vector3f loc, Spatial model, String mats )
    {
        Spatial temp;
        if (!mats.equals("01ddf"))
        {
            temp = model;
            try
            {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document d = db.newDocument();
                d = db.parse(new File(mats));
                NodeList texture = d.getElementsByTagName("texture");
                NodeList bump = d.getElementsByTagName("bump");
                NodeList parallax = d.getElementsByTagName("parallax");
                Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
                //mat.setBoolean("UseMaterialColors", true);
                mat.setTexture("DiffuseMap", lund.loadTexture(texture.item(0).getTextContent().trim() + ".png"));
                if (bump.getLength() > 0)
                {
                    mat.setTexture("NormalMap", lund.loadTexture(bump.item(0).getTextContent().trim() + ".png"));
                }
                if (parallax.getLength() > 0)
                {
                    mat.setTexture("ParallaxMap", lund.loadTexture(parallax.item(0).getTextContent().trim() + ".png"));
                }
                mat.setColor("Specular", ColorRGBA.White);
                mat.setFloat("Shininess", 12);
                mat.setBoolean("VertexLighting", false);
                temp.setMaterial(mat);
                temp.setLocalTranslation(loc);
            } 
            catch (Throwable e)
            {
                lund.printConsoleWarning(e.getLocalizedMessage());
                lund.printConsoleWarning(e.getMessage());
            }
        }
        else
        {
            temp = model;
            Material mat = new Material(app.getAssetManager(),"Common/MatDefs/Light/Lighting.j3md");
            mat.setColor("Diffuse", ColorRGBA.Magenta);
            mat.setColor("Specular", ColorRGBA.White);
            mat.setFloat("Shininess", 12);
            mat.setBoolean("VertexLighting", false);
            temp.setMaterial(mat);
            temp.setLocalTranslation(loc);
        }
        return temp;
    }
    
    /*
    pass_spawn:
    spawns the object the player has selected
    */
    public void passSpawn(SimpleApplication app)
    {
        GameInterface gi = Main.getGI();
        Node ti = Main.appref.getRootNode();
        Vector3f origin = app.getCamera().getWorldCoordinates(new Vector2f(app.getCamera().getWidth() / 2, app.getCamera().getHeight() / 2), 0.0f);
        Vector3f direction = app.getCamera().getWorldCoordinates(new Vector2f(app.getCamera().getWidth() / 2, app.getCamera().getHeight() / 2), 0.3f);
        direction.subtractLocal(origin).normalizeLocal();
        Ray ray = new Ray(origin, direction);
        CollisionResults results = new CollisionResults();
        int numCollisions = ti.collideWith(ray, results);
        if (numCollisions > 0) 
        {
            CollisionResult hit = results.getClosestCollision();
            Vector3f hitPos = new Vector3f(hit.getContactPoint().x,hit.getContactPoint().y + Main.player.choiceSize,
                    hit.getContactPoint().z);
            int choice = gi.curbar;
            switch(choice)
            {
                case 1: Main.rendernode.attachChild(spawnBox(app,hitPos));
                    break;
                case 2: Main.rendernode.attachChild(spawnSphere(app,hitPos));
                    break;
                case 3: Main.rendernode.attachChild(spawnCylinder(app,hitPos));
                    break;
                default: System.out.println("No object selected, This shouldn't occur." + choice);
                    break;
            }
        }
    }
}
